<template>
	<view>
		<view class="" style="background-color: #fff;">
			<!-- <u-sticky > -->
			<u-tabs :list="tops" :activeStyle="{
					    color: '#303133',
					    fontWeight: 'bold',
					    transform: 'scale(1.05)'
					}" :inactiveStyle="{
					    color: '#606266',
					    transform: 'scale(1)'
					}" @click="stateFunc" lineColor="#BF6FDC" lineActive='-4rpx'></u-tabs>
			<!-- </u-sticky> -->
		</view>
		<!-- <view class="top-tabbar">
			
			<u-tabs :list="tops" :activeStyle="{
				    color: '#303133',
				    fontWeight: 'bold',
				    transform: 'scale(1.05)'
				}" :inactiveStyle="{
				    color: '#606266',
				    transform: 'scale(1)'
				}" @click="stateFunc" lineColor="#00C8BE" lineActive='-4rpx'></u-tabs>
		</view> -->
		<!--列表-->
		<scroll-view scroll-y="true" class="scroll-Y" lower-threshold="50" @scrolltolower="scrolltolowerFunc">
			<view :class="topRefresh ? 'top-refresh open' : 'top-refresh'">
				<view class="circle" v-for="(circle, n) in 3" :key="n"></view>
			</view>
			<view class="order-list">
				<view class="item" v-for="(item, index) in listData" :key="index">
					<view class="order-head d-b-c">
						<view>
							<text class="shop-name flex-1 fb">下单时间：{{ item.create_time }}</text>
						</view>
						<view class="state">
							<text class="red">{{ item.state_text }}</text>
						</view>
					</view>
					<!--多个商品显示-->

					<view class="product-list pr" v-if="item.product.length > 1" @click="jumpPage(item.order_id)">
						<scroll-view scroll-x="true">
							<view class="list d-s-c pr100">
								<view class="cover mr10" v-for="(img, num) in item.product" :key="num">
									<image :src="img.image.file_path" mode="aspectFit"></image>
								</view>
							</view>
						</scroll-view>
						<view class="total-count">
							<view class="left-shadow"></view>
							<view class="price f22">
								¥
								<text class="f40">{{ item.pay_price}}</text>
							</view>
							<view class="count">共{{ item.product.length }}件</view>
						</view>
					</view>
					<!--一个商品显示-->
					<view class="one-product d-s-c" v-else @click="jumpPage(item.order_id)">
						<view class="cover" v-for="(img, num) in item.product" :key="num">
							<image :src="img.image.file_path" mode="aspectFit"></image>
						</view>
						<!-- <view class="pro-info flex-1">{{ item.product[0].product_name }}</view> -->
						<view class="pro-info1 flex-1">
							<view class="">
								{{ item.product[0].product_name }}
							</view>
							<view class="">
								{{ item.product[0].product_attr }}
							</view>
							<view class="">
								租期：{{ item.start_time }} 至 {{ item.end_time }} （{{ item.total_day }}天）
							</view>
							<view class="">
								总租金：{{ item.bill_price }}
							</view>
							<view class="">
								到期买断价：{{ item.product[0].buy_out_price ? item.product[0].buy_out_price : '--' }}
							</view>
							<view class="">
								押金信息：{{item.deposit ? item.deposit : '--'}}
							</view>
							<view class="">
								支付金额：{{item.bill_pay_price}}
							</view>
						</view>
					</view>
					<view class="" style="margin-bottom: 20rpx;">
						<text class="shop-name flex-1 fb" style="color: rgb(56, 55, 55);">订单号：{{ item.order_no }}</text>
					</view>
					<view class="order-bts">
						<!-- 取消订单 -->
						<block
							v-if="item.pay_status.value == 20 &&item.delivery_status.value==10&&item.order_status.value == 10">
							<button @click="openClose(item.order_no)" type="default">取消订单</button>
						</block>
						<block v-if="">
							<button style="background-color: rgb(188, 97, 230);border: none;color: #fff;"
								@click="certifi(item.order_id)">发起公证</button>
						</block>
						<!-- 订单去发货 -->
						<block
							v-if="item.pay_status.value == 20 && item.delivery_type.value == 10 && item.order_status.value==10&&item.delivery_status.value == 10">
							<button style="color: rgb(51, 196, 181); border: 1px solid rgb(51, 196, 181)"
								@click="deliveryClick(item.order_id)">去发货</button>
						</block>
						
						<block>
							<button style="background-color: rgb(224, 147, 147);border: none;color: #fff;"
								@click="goRisk(item.order_id)">查看风控</button>
						</block>
						<block v-if="item.shop_url == '' && item.order_status.value == 10">
							<button style="background-color: rgb(69, 201, 138);border: none;color: #fff;"
								@click="createEqb(item.order_id)">生成合同</button>
						</block>
						<block v-if="item.shop_url !== ''">
							<button style="background-color: rgb(69, 201, 138);border: none;color: #fff;"
								@click="createEqbLook(item.shop_url)">查看合同</button>
						</block>
						<block v-if="item.order_status.value==1">
							<button style="color: #44B549;" @click="toExamine(item.order_id,2)">订单通过</button>
							<button class="btn-red-border" @click="toExamine(item.order_id,20)">订单驳回</button>
						</block>
					</view>
					<view class="" style="margin: 20rpx 0;color: rgb(56, 55, 55);">
						<text class="shop-name flex-1 fb">{{item.address.name}}</text>
						<text class="shop-name flex-1 fb" style="margin-left: 10rpx;">{{item.address.phone}}</text>
					</view>
					<view class="" style="margin-bottom: 20rpx;color: rgb(120, 119, 119);">
						<text
							class="shop-name flex-1 fb">{{item.address.region.province}},{{item.address.region.city}},{{item.address.region.region}},{{item.address.detail}}</text>
					</view>
					<view class="order-bts">
						<block>
							<button style="color: #fff;background-color: rgb(70, 166, 174);border: none;"
								@click="clipboardData(item)">复制地址</button>
							<button style="color: #fff;background-color: rgb(70, 166, 174);border: none;"
								@click="call(item.address.phone)">联系收件人</button>
						</block>
					</view>
				</view>
				<view class="d-c-c p30" v-if="listData.length == 0 && !loading">
					<text class="iconfont icon-wushuju"></text>
					<text class="cont">亲，暂无相关记录哦</text>
				</view>
				<uni-load-more v-else :loadingType="loadingType"></uni-load-more>
			</view>
		</scroll-view>
		<u-popup :show="isClose" mode="center" :round="10" closeable @close="hideClose">
			<view class="ww100 p20 box-s-b" style="padding: 50rpx 60rpx;">
				<view class="f32 fb mb20">取消订单</view>
				<view class="d-s-c mb20">
					<view class="w120 f26">订单号：</view>
					<view>{{order_no}}</view>
				</view>
				<view class="d-s-s mb20">
					<view class="w120 f26">备注：</view>
					<textarea v-model="cancel_remark" placeholder="请输入备注" class="border p10 w400" />
				</view>
				<!-- 		<view class="d-c-c">
					<button class="send_btn btn-gray-border mr30" @click="hideClose">取消</button>
					<button class="send_btn btn-orange" @click="sendClose()">确定</button>
				</view> -->
				<view style='display: flex;width: 100%;justify-content: space-around;align-items: center;'>
					<button style="color: rgb(42, 42, 42);width: 50%;border: none;" @click="hideClose">取消</button>
					<button style="color: rgb(70, 166, 174);width: 50%;border: none;" @click="sendClose">确定</button>
				</view>
			</view>
		</u-popup>
		<u-popup :show="deliveryShow" @close="deliveryClose" mode="center" :round="10" closeable>
			<view style="padding: 60rpx 80rpx;">
				<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm">
					<u-form-item label="发货方式" borderBottom ref="item1" labelWidth="80">
						<u-radio-group v-model="model1.is_delivery">
							<u-radio shape="circle" label="物流发货" :name='10' style="margin-right: 10rpx;"></u-radio>
							<u-radio shape="circle" label="门店自提" :name='20'></u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="物流公司" prop="express_id" borderBottom @click="hideKeyboard()" labelWidth="80"
						v-if="model1.is_delivery == 10" ref="item1">
						<u--input v-model="model1.express_name" placeholder="请选择物流公司" @click="hideKeyboard()"
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right" @click="hideKeyboard()"></u-icon>
					</u-form-item>
					<u-form-item label="物流单号" prop="express_no" borderBottom ref="item1" labelWidth="80"
						v-if="model1.is_delivery == 10">
						<u--input v-model="model1.express_no" border="none"></u--input>
					</u-form-item>
					<u-form-item label="设备串码号" prop="equipment_no" borderBottom ref="item1" labelWidth="80">
						<u--input v-model="model1.equipment_no" border="none"></u--input>
					</u-form-item>
				</u--form>
				<view style='display: flex;width: 100%;justify-content: space-around;align-items: center;'>
					<button style="color: rgb(42, 42, 42);width: 50%;border: none;" @click="deliverSubmit">发货</button>
					<button style="color: rgb(70, 166, 174);width: 50%;border: none;" @click="deliveryClose">取消</button>
				</view>
			</view>
		</u-popup>
		<u-popup :show="certifiShow" @close="certifiClose" mode="bottom" :round="10" closeable>
			<view style='width: 100vw;'>
				<view class="" style="height: 50rpx;"> </view>
				<button style="color: rgb(70, 166, 174);width: 80%;border: none; margin: 0 auto;"
					@click="certifiEvent(1)">凤凰公证</button>
				<button style="color: rgb(70, 166, 174);width: 80%;border: none;margin: 0 auto;"
					@click="certifiEvent(2)">鹭江公证</button>
				<view class="" style="height: 40rpx;"> </view>
			</view>
		</u-popup>
		<u-action-sheet :show="expressShow" :actions="expressList" title="请选择物流公司" @close="expressShow = false"
			:round='10' @select="sexSelect">
		</u-action-sheet>

	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more.vue';
	import Popup from '@/components/uni-popup.vue';
	export default {
		components: {
			uniLoadMore,
			Popup
		},
		data() {
			return {
				expressList: [],
				expressShow: false,
				certifiShow: false,
				model1: {
					is_delivery: 10,
					equipment_no: "",
					express_no: "",
					express_id: "",
					express_name: ""
				},
				rules: {
					express_id: [{
						required: true,
						message: '请选择发货方式',
						trigger: 'blur'
					}],
					express_no: [{
						required: true,
						message: '请输入物流单号',
						trigger: 'blur'
					}],
					equipment_no: [{
						required: true,
						message: '请输入设备串码号',
						trigger: 'blur'
					}]
				},
				deliveryShow: false,
				tops: [{
					name: '全部订单',
					id: 0
				}, {
					name: '待付款',
					id: 1
				}, {
					name: '待审核',
					id: 5
				}, {
					name: '待发货',
					id: 2
				}, {
					name: '待收货',
					id: 3
				}, {
					name: '租赁中',
					id: 6
				}, {
					name: '待归还',
					id: 7
				}, {
					name: '归还中',
					id: 9
				}, {
					name: '已完成',
					id: 10
				}, {
					name: '取消中',
					id: 13
				}, {
					name: '已取消',
					id: 8
				}, {
					name: '已驳回',
					id: 11
				}, {
					name: '已逾期',
					id: 12
				}],
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				/*状态选中*/
				state_active: 0,
				/*顶部刷新*/
				topRefresh: false,
				/*数据*/
				listData: [],
				/*数据类别*/
				dataType: 'all',
				/*订单id*/
				order_id: 0,
				/*最后一页码数*/
				last_page: 0,
				/*当前页面*/
				page: 1,
				/*每页条数*/
				list_rows: 10,
				/*有没有等多*/
				no_more: false,
				/*是否正在加载*/
				loading: true,
				shop_supplier_id: '',
				isClose: false,
				cancel_remark: '',
				order_no: ''
			};
		},
		computed: {
			/*加载中状态*/
			loadingType() {
				if (this.loading) {
					return 1;
				} else {
					if (this.listData.length != 0 && this.no_more) {
						return 2;
					} else {
						return 0;
					}
				}
			}
		},
		onLoad(e) {
			this.shop_supplier_id = e.shop_supplier_id;
			if (typeof e.dataType != 'undefined') {
				this.dataType = e.dataType;
			}
			if (this.dataType == 'payment') {
				this.state_active = 1;
			} else if (this.dataType == 'delivery') {
				this.state_active = 2;
			} else if (this.dataType == 'received') {
				this.state_active = 3;
			}
		},
		mounted() {
			this.init();

		},
		onShow() {
			this.initData();
			/*获取订单列表*/
			this.getData();
		},
		methods: {
			goRisk(id){
				this.gotoPage('/pages/user/my_shop/risk?order_id=' + id);
			},
			hideKeyboard() {
				let self = this
				uni.showLoading({
					title: '正在处理'
				});
				self._get(
					'supplier.order/expressList', {},
					function(res) {
						uni.hideLoading();
						console.log(res)
						self.expressShow = true;
						// self.expressList = res.data.list
						self.expressList = res.data.list.map(item => ({
							express_id: item.express_id,
							name: item.express_name
						}));
					}
				);
			},
			sexSelect(e) {
				this.model1.express_id = e.express_id
				this.model1.express_name = e.name
			},
			certifiEvent(num) {
				let self = this
				uni.showLoading({
					title: '正在提交'
				});
				if (num == 1) {
					self._post(
						'fuqiang.index/createApply', {
							order_id: self.order_id
						},
						function(res) {
							uni.hideLoading();
							self.certifiShow = false
							self.showSuccess(res.msg);
							self.getData();
						}
					);
				} else {
					self._post(
						'lujiang.index/createOrder', {
							order_id: self.order_id
						},
						function(res) {
							uni.hideLoading();
							self.certifiShow = false
							self.showSuccess(res.msg);
							self.getData();
						}
					);
				}
			},
			certifiClose() {
				this.certifiShow = false
			},
			// 发起公证
			certifi(id) {
				this.order_id = id
				this.certifiShow = true
			},
			// 查看合同
			createEqbLook(url) {
				my.ap.openURL({
					url: url,
					success: (res) => {
						console.log('openURL success', res)
					},
					fail: (err) => {
						console.log('openURL success', err)
					}
				});
			},
			// 生成合同
			createEqb(id) {
				let self = this
				uni.showLoading({
					title: '正在处理'
				});
				self._post(
					'order.contract/createByDocTemplate', {
						order_id: id,
						app_id: 10001
					},
					function(res) {
						uni.hideLoading();
						self.showSuccess(res.msg);
						self.getData();
					}
				);
			},
			deliverSubmit() {
				if (this.model1.express_id == '' || this.model1.express_no == '' || this.model1.equipment_no == '') {
					uni.$u.toast('请将信息填写完整！')
					return
				}
				console.log(this.model1)
				let self = this;
				let param = self.model1;
				uni.showLoading({
					title: '正在处理'
				});
				self._post(
					'supplier.order/delivery', {
						order_id: self.order_id,
						param: JSON.stringify(param),
						is_delivery: param.is_delivery,
					},
					function(res) {
						uni.hideLoading();
						self.deliveryShow = false
						self.showSuccess(res.msg);
						self.getData();
					}
				);

			},
			deliveryClose() {
				this.deliveryShow = false
				console.log('close');
			},
			deliveryClick(id) {
				this.order_id = id
				this.deliveryShow = true
			},
			// 复制地址
			clipboardData(item) {
				uni.setClipboardData({
					data: `${item.address.region.province},${item.address.region.city},${item.address.region.region},${item.address.detail}`,
					success: function() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						});
					},
					fail: function(err) {
						console.log('复制失败', err);
					}
				});
			},
			//联系收件人
			call(phone) {
				const self = this;
				// 拨打电话
				uni.makePhoneCall({
					phoneNumber: phone,
				});
			},
			toExamine(id, type) {
				let self = this;
				self.loading = true;
				self._post(
					'supplier.order/toExamine', {
						order_id: id,
						status: type
					},
					function(res) {
						self.loading = false;
						self.initData();
						self.getData();
					}
				);
			},
			/*初始化*/
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						// 计算组件的高度
						let view = uni.createSelectorQuery().select('.top-tabbar');
						view.boundingClientRect(data => {
							let h = self.phoneHeight - data.height;
							self.scrollviewHigh = h;
						}).exec();
					}
				});
			},
			initData() {
				let self = this;
				self.page = 1;
				self.loading = true;
				self.listData = [];
				self.no_more = false;
			},
			/*状态切换*/
			stateFunc(e) {
				let self = this;
				if (self.state_active != e.index) {
					self.page = 1;
					self.loading = true;
					self.state_active = e.index;
					switch (e.index) {
						case 0:
							self.listData = [];
							self.dataType = 'all';
							break;
						case 1:
							self.listData = [];
							self.dataType = 'payment';
							break;
						case 2:
							self.listData = [];
							self.dataType = 'delivery';
							break;
						case 3:
							self.listData = [];
							self.dataType = 'received';
							break;
						case 4:
							self.listData = [];
							self.dataType = 'comment';
							break;
					}
					console.log(self.listData)
					self.getData();
				}
			},

			/*可滚动视图区域到底触发*/
			scrolltolowerFunc() {
				let self = this;
				if (self.no_more) {
					return;
				}
				self.page++;
				if (self.page <= self.last_page) {
					self.getData();
				} else {
					self.no_more = true;
				}
			},

			/*获取数据*/
			getData() {
				let self = this;
				self.loading = true;
				let dataType = self.dataType;
				self._get(
					'supplier.order/index', {
						shop_supplier_id: self.shop_supplier_id,
						dataType: dataType,
						page: self.page,
						list_rows: self.list_rows,
						pay_source: self.getPlatform()
					},
					function(res) {
						self.loading = false;
						self.listData = self.listData.concat(res.data.list.data);
						self.last_page = res.data.list.last_page;
						if (res.data.list.last_page <= 1) {
							self.no_more = true;
						} else {
							self.no_more = false;
						}
					}
				);
			},

			/*跳转页面*/
			jumpPage(e) {
				this.gotoPage('/pages/order/order-detail?source=supplier&order_id=' + e);
			},
			/* 打开取消订单 */
			openClose(e) {
				this.isClose = true;
				this.order_no = e;
			},
			sendClose() {
				let self = this;
				let order_no = self.order_no;
				wx.showModal({
					title: '提示',
					content: '您确定要取消订单吗?',
					success: function(o) {
						if (o.confirm) {
							self.isClose = false;
							uni.showLoading({
								title: '正在处理'
							});
							self._get(
								'supplier.order/orderCancel', {
									order_no: order_no,
									cancel_remark: self.cancel_remark
								},
								function(res) {
									uni.hideLoading();
									uni.showToast({
										title: '操作成功',
										duration: 2000,
										icon: 'success'
									});
									self.listData = [];
									self.getData();
								}
							);
						}
					}
				});
			},
			/*关闭取消订单 */
			hideClose(e) {
				this.isClose = false;
			},
		}
	};
</script>

<style lang="scss" scoped>
	.order-list .order-head .state-text {
		padding: 4rpx 8rpx;
		margin-right: 10rpx;
		border-radius: 4rpx;
		background: #e2231a;
		color: #ffffff;
	}

	.order-list .item {
		margin-top: 30rpx;
		padding: 30rpx;
		background: #ffffff;
	}

	.order-list .product-list,
	.order-list .one-product {
		padding: 20rpx 0;
		// height: 160rpx;
	}

	.one-product .pro-info1 {
		// padding: 0 30rpx;
		// display: -webkit-box;
		// overflow: hidden;
		// -webkit-line-clamp: 2;
		// -webkit-box-orient: vertical;
		font-size: 22rpx;
		padding: 0 20rpx 0 30rpx;
		color: #666666;
	}

	.one-product .pro-info1 view {
		margin-bottom: 10rpx;
	}

	.order-list .cover,
	.order-list .cover image {
		width: 160rpx;
		height: 160rpx;
	}

	.order-list .total-count {
		padding-left: 20rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;
	}

	.total-count .count {
		padding-top: 10rpx;
		color: #666;
		font-size: 28rpx;
	}

	.product-list .total-count {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		background: rgba(255, 255, 255, 0.9);
	}

	.product-list .total-count .left-shadow {
		position: absolute;
		top: 0;
		bottom: 0;
		left: -24rpx;
		width: 24rpx;
		overflow: hidden;
	}

	.product-list .total-count .left-shadow::after {
		position: absolute;
		top: 0;
		bottom: 0;
		width: 24rpx;
		right: -12rpx;
		display: block;
		content: '';
		background-image: radial-gradient(rgba(0, 0, 0, 0.2) 10%, rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0) 80%);
	}

	.order-list .order-bts {
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.order-list .order-bts button {
		margin: 0;
		padding: 0 30rpx;
		height: 60rpx;
		line-height: 60rpx;
		margin-left: 20rpx;
		border-radius: 30rpx;
		font-size: 24rpx;
		border: 1px solid #cccccc;
		white-space: nowrap;
		background: #ffffff;
	}

	.order-list .order-bts button::after {
		display: none;
	}

	.order-list .order-bts button.btn-border-red {
		border: 1px solid $dominant-color;
		font-size: 24rpx;
		color: $dominant-color;
	}

	.order-list .order-bts button.btn-red {
		background: $dominant-color;
		border: 1px solid $dominant-color;
		font-size: 24rpx;
		color: #ffffff;
	}

	.buy-checkout {
		width: 100%;
	}

	.buy-checkout .item {
		min-height: 50rpx;
		line-height: 50rpx;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
	}

	.buy-checkout .iconfont.icon-weixin {
		color: #04be01;
		font-size: 50rpx;
	}

	.buy-checkout .iconfont.icon-yue {
		color: #f0de7c;
		font-size: 50rpx;
	}

	.buy-checkout .item.active .iconfont.icon-xuanze {
		color: #04be01;
	}

	.item-dianpu {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 30rpx;
		font-size: 24rpx;
		line-height: 30rpx;
	}

	.item-d-l {
		display: flex;
	}

	.icon-dianpu1 {
		margin-right: 30rpx;
	}

	.top-tabbar {
		width: 100vw;
	}
</style>